﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Exceptionless;
using IdentityServer4.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Utility.Extensions;

namespace QuickFramework.UICore.Controllers.User
{
    /// <summary>
    /// 用户登录/帐号认证Token颁发
    /// </summary>
    [Area("User")]
    [AllowAnonymous]
    public class AccountController : Shared.Contracts.AuthorizeControllerBase
    {
        Users.IDomain.IAuthenticate m_IAuthenticate;
        Users.IDomain.IUserInfo m_IUserInfo;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="authenticate"></param>
        /// <param name="userInfo"></param>
        public AccountController(Users.IDomain.IAuthenticate authenticate
            , Users.IDomain.IUserInfo userInfo
            )
        {
            m_IAuthenticate = authenticate;
            m_IUserInfo = userInfo;
        }
        /// <summary>
        /// 测试日志系统
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> TestLog()
        {
            //return Ok();
            throw new Exception("测试日志系统");
        }
        /// <summary>
        /// 测试日志系统
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult TestLog2()
        {
            try
            {
                throw new Exception("测试主动抛送异常日志系统");
            }
            catch (Exception ex)
            {
                ex.ToExceptionless().AddTags("Test").Submit();
            }
            return Ok(new { Result = "测试完成" });
        }
        /// <summary>
        /// 用户登录
        /// url: /api/User/Account/Login
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Login(Users.DTO.User user)
        {
            //增加 用户 信息校验逻辑
            var result = await m_IUserInfo.Login(user);
            if (result.Item1)
            {
                user.ID = result.Item3;
                user.Password = "";
                //认证通过,创建Token
                var jwtToken = m_IAuthenticate.TokenAuthenticated(user);
                return Ok(new { Result = result.Item1, Token = jwtToken });
            }
            else
            {
                return Ok(new { Result = result.Item1, Msg = result.Item2 });
            }
        }
        /// <summary>
        /// Token 刷新
        /// url: /api/User/Account/RefreshToken
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HttpGet]
        [Authorize]
        public ActionResult RefreshToken()
        {
            Users.DTO.User user = new Users.DTO.User
            {
                ID = this.User.Claims.FirstOrDefault(m => m.Type.Equals(ClaimTypes.NameIdentifier)).Value,
                Username = User.GetDisplayName()
            };

            var jwtToken = m_IAuthenticate.TokenAuthenticated(user);
            return Ok(jwtToken);
        }

        /// <summary>
        /// 用户登出
        /// url: /api/User/Account/Logout
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [HttpGet]
        public ActionResult Logout()
        {
            return Ok("success");
        }
    }
}